/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.CTPhieuThuePhong_DTO;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Lenh Ho Xung
 */
public class CTPhieuThuePhong_DAO {

    private static String sqlString;
    private static MSSQLConnect cn;
    private static ResultSet rs;

    public static String laymaMax() throws Exception {
        String maphieuMax = new String();
        sqlString = "SELECT TOP 1 MaCTPhieuThue FROM CTPhieuThuePhong ORDER BY MaCTPhieuThue DESC";
        try {
            cn = new MSSQLConnect();
            rs = cn.excuteQuery(sqlString);
            while (rs.next()) {
                maphieuMax = rs.getString("MaCTPhieuThue");
            }
        } catch (Exception e) {
            throw new Exception("Không thể lấy mã max");

        } finally {
            cn.Close();
        }
        return maphieuMax;
    }

    public static int Insert(CTPhieuThuePhong_DTO p) throws Exception {
        int n;
        sqlString = "INSERT INTO CTPHIEUTHUEPHONG (MaCTPhieuThue, MaPhieuThue ,MaPhong) VALUES('"
                + p.getMactphieuThue() + "','"
                + p.getMaphieuThue() + "','"
                + p.getMaPhong() + "')";

        //Kết nối CSDL
        try {
            cn = new MSSQLConnect();
            n = cn.executeUpdate(sqlString);
        } catch (Exception e) {
            throw new Exception("Không thể thêm vào cơ sơ dữ liệu");
        } finally {
            cn.Close();
        }
        return n;
    }

    public static ArrayList<CTPhieuThuePhong_DTO> layDSPhongThue(String maKhach) throws Exception {
        ArrayList<CTPhieuThuePhong_DTO> ds = new ArrayList<>();
        sqlString = "Select MaPhong from CTPHIEUTHUEPHONG where MaPhieuThue = "
                + "(select MaPhieuThue from PHIEUTHUEPHONG where MaKhach = '" + maKhach + "')";
        try {
            //thực hiện câu truy vấn
            cn = new MSSQLConnect();
            //thực hiện câu truy vấn
            rs = cn.excuteQuery(sqlString);
            while (rs.next()) {
                CTPhieuThuePhong_DTO c = new CTPhieuThuePhong_DTO();
                c.setMaPhong(rs.getString("MaPhong"));
                ds.add(c);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            throw new Exception("Không thể lấy danh sách mã dịch vụ");
        } finally {
            cn.Close();
        }
        return ds;
    }
}
